package models

import (
	"gintest/database"
)
type ParentArticleClass struct{
	Id        int    `json:"id" gorm:"column:id;primary_key" form:"id"`
	Title string `json:"title" gorm:"column:title" form:"title"`
	Sort  int `json:"sort" gorm:"column:sort" form:"sort"`
}
func (ParentArticleClass) TableName() string {
	return "article_class"
}
//表结构
type ArticleClass struct {
	Id        int    `json:"id" gorm:"column:id;primary_key" form:"id"`
	ParentId  int `json:"parent_id" gorm:"column:parent_id" form:"parent_id"`
	Title string `json:"title" gorm:"column:title" form:"title"`
	Sort  int `json:"sort" gorm:"column:sort" form:"sort"`

	ParentArticleClass ParentArticleClass `json:"parent_article_class" gorm:"ForeignKey:ParentId"`
}
func (ArticleClass) TableName() string {
	return "article_class"
}

func GetArticleClassList(page uint64,pagesize uint64,keyword string)(data []ArticleClass,total uint64){
	var offset=(page-1)*pagesize
	db:=database.SqlDB.Model(ArticleClass{})

	if len(keyword)!=0{
		db=db.Where("title like ?","%"+keyword+"%")
	}

	db.Offset(offset).Limit(pagesize).Order("id desc").Preload("ParentArticleClass").Find(&data)
	db.Model(ArticleClass{}).Count(&total)

	return data,total
}
func AddArticleClass(data *ArticleClass) error{
	return database.SqlDB.Model(ArticleClass{}).Omit("ParentArticleClass").Create(data).Error
}
func DelArticleClassByIds(ids []int) error{
	return database.SqlDB.Model(ArticleClass{}).Where("id in (?)",ids).Delete(ArticleClass{}).Error
}
func GetArticleClassById(id int) (data ArticleClass){
	database.SqlDB.Model(ArticleClass{}).Where("id=?",id).Preload("ParentArticleClass").First(&data);
	return data
}
func EditArticleClassById(id int,data interface{}) error{
	return database.SqlDB.Model(&ArticleClass{}).Where("id=?",id).Updates(data).Error
}